<?php

namespace App\Services\Discovery;

use App\Models\Store\Type;
use App\Models\Store\Store;
use App\Models\Common\City;
use App\Services\Com\UtilService;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class DiscoveryService
{
    public static function storeList(array $param = []){

        $where[] = ['state', '=', 1];
        
        $model = Store::select('uuid','member_uuid','title','head_img','type_uuid',
            'content','tag_uuid','place_city_code','address','is_top','is_top_time','place','created_at');
        
        // $model->with(['file' => function ($query) {
        //     return $query->select(['uuid', 'file'])->where('state','=','1');
        // }]);

        $model->with(['type' => function ($query) {
            return $query->select(['uuid', 'name']);
        }]);

        $model->with(['tag' => function ($query) {
            return $query->select(['uuid', 'title']);
        }]);

        // 传市或者区
        if($param['place_city_code']){
            $cidy_arr = UtilService::getCityId($param['place_city_code']);
            $model->whereIn('place_city_code', $cidy_arr);
        }

        if($param['tag_uuid']){
            $where[] = ['tag_uuid','=',$param['tag_uuid']];
        }
        if($param['type_uuid']){
            $where[] = ['type_uuid','=',$param['type_uuid']];
        }
        
        !empty($param['keywords']) && $model->where('title','like', "%{$param['keywords']}%");
        $data = $model->where($where)
                ->orderBy('is_top', 'desc')
                ->orderBy('is_top_time', 'desc')
                ->orderBy('id', 'desc')
                ->paginate($param['limit']);

        $res = [];
        foreach ($data as $key => $value) {
            $file_arr = $value->file->toArray();
            $file_arr_res = [];
            $store_res = $value->toArray();
            if(!empty($file_arr)){
                foreach ($file_arr as $key2 => $value2) {
                    $file_arr_res[] = [
                                        'store_uuid'=>$store_res['uuid'],
                                        'file'=>[
                                                'coverURL' => UtilService::coverURL($value2['path']),
                                                'playURL' => $value2['path']
                                            ],
                                        'file_type'=>$value2['type'] == 1 ? 'image' : 'video',
                                        
                                        ];
                }
            }
            $res[$key]['data'] = $store_res;
            
            $city_arr = City::getCityById($res[$key]['data']['place_city_code']);
            $path_names_arr = null;
            if(!empty($city_arr)){
                $path_names_arr = explode('/', $city_arr['path_names']);
            }
            $res[$key]['data']['place_level_1'] = $path_names_arr[0] ?? null; //省
            $res[$key]['data']['place_level_2'] = $path_names_arr[1] ?? null; //市
            $res[$key]['data']['place_level_3'] = $path_names_arr[2] ?? null; //区
            $res[$key]['data']['place_city_name'] = $city_arr['name'] ?? null;//城市code

            $res[$key]['data']['file'] = $file_arr_res;
        }
        $res_arr = $data->toArray();

        $list['data'] = $res;
        $list['total'] = $res_arr['total'];
        return $list;
    }

}